package com.android.quiz.geek;

import java.util.ArrayList;
import java.util.Random;

import com.android.quiz.geek.quiz.Question;
import com.android.quiz.geek.quiz.Questions;
import com.android.quiz.geek.utils.InitQuestion;
import com.android.quiz.geek.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class GQQuizActivity extends Activity
{
	private Questions		q;
	private TextView		questionText;
	private RadioButton	reponse1;
	private RadioButton	reponse2;
	private RadioButton	reponse3;
	private RadioButton	reponse4;
	private Button			valider;
	private int					GoodReponseNumber;
	private int					reponseNumber;
	private String			goodRep;
	private String			selectedRep;
	private Random			mRnd	= new Random();

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quizz);

		inItializeBtn();

		intializeQuiz();

		initFirstQuestion();

		// validateQuestion();

	}

	private void validateQuestion()
	{

		getGoodReponse();
		Utils.DisplayToast(getBaseContext(), "goodRep = " + goodRep, true);
		if (selectedRep.equals(goodRep) == true)
		{
			Utils.DisplayToast(getBaseContext(), "Bonne reponse", true);
			++GoodReponseNumber;
		}
		else
		{
			Utils.DisplayToast(getBaseContext(), "Mauvaise reponse", false);
		}

		--reponseNumber;
		if (reponseNumber != 0)
		{
			nextQuestion();
		}
		else
		{
			Intent intent = new Intent(GQQuizActivity.this, GQQuizResActivity.class);
			intent.putExtra("GoodReponseNumber", this.GoodReponseNumber);
			startActivity(intent);
		}
	}

	private void getGoodReponse()
	{
		if (reponse1.isChecked() == true)
		{
			selectedRep = reponse1.getText().toString();
		}
		else
			if (reponse2.isChecked() == true)
			{
				selectedRep = reponse2.getText().toString();
			}
			else
				if (reponse3.isChecked() == true)
				{
					selectedRep = reponse3.getText().toString();
				}
				else
					if (reponse4.isChecked() == true)
					{
						selectedRep = reponse4.getText().toString();
					}
		Utils.DisplayToast(getBaseContext(), "selectedRep = " + selectedRep, true);
	}

	private void initFirstQuestion()
	{
		int index = mRnd.nextInt(reponseNumber);
		Utils.DisplayToast(getBaseContext(), "random = " + index, true);
		Question qst = q.getQuestion(index);
		questionText.setText(qst.getQuestion());
		ArrayList<String> rep = new ArrayList<String>();
		rep.addAll(qst.getReponse());
		// int indexRep = mRnd.nextInt(4);
		reponse1.setText(rep.get(0));
		reponse2.setText(rep.get(1));
		reponse3.setText(rep.get(2));
		reponse4.setText(rep.get(3));
		goodRep = qst.getGoodReponse();
	}

	private void nextQuestion()
	{
		if (reponseNumber == 1)
		{
			this.valider.setText("Terminer");
			Utils.DisplayToast(getBaseContext(), "Quiz Terminer", false);
		}

		mRnd = null;
		mRnd = new Random();
		int index = mRnd.nextInt(reponseNumber);
		Utils.DisplayToast(getBaseContext(), "random = " + index, true);
		Question qst = q.getQuestion(index);
		questionText.setText(qst.getQuestion());
		ArrayList<String> rep = new ArrayList<String>();
		rep.addAll(qst.getReponse());
		// int indexRep = mRnd.nextInt(4);
		reponse1.setText(rep.get(0));
		reponse2.setText(rep.get(1));
		reponse3.setText(rep.get(2));
		reponse4.setText(rep.get(3));
		goodRep = qst.getGoodReponse();

	}

	private void inItializeBtn()
	{
		GoodReponseNumber = 0;
		selectedRep = "";
		questionText = (TextView) findViewById(R.quiz.question);
		reponse1 = (RadioButton) findViewById(R.quiz.reponse1);
		reponse2 = (RadioButton) findViewById(R.quiz.reponse2);
		reponse3 = (RadioButton) findViewById(R.quiz.reponse3);
		reponse4 = (RadioButton) findViewById(R.quiz.reponse4);
		valider = (Button) findViewById(R.quiz.validate);
		valider.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				validateQuestion();
			}
		});
	}

	private void intializeQuiz()
	{
		q = new Questions();
		q = InitQuestion.generateQuestions();
		reponseNumber = q.questionNumber();
	}
}
